package com.conferenceroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.conferenceroom.dto.AjaxResult;
import com.conferenceroom.dto.CustomerReport;
import com.conferenceroom.entity.Record;
import com.conferenceroom.mapper.RecordMapper;
import com.conferenceroom.service.RecordService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {

    @Override
    public List<Record> getRecordsByUserId(Integer userId) {
        QueryWrapper<Record> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Record> getRecordsByRoomId(Integer roomId) {
        QueryWrapper<Record> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", roomId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Record> list(QueryWrapper<Record> queryWrapper) {
        return baseMapper.selectList(queryWrapper);
    }

    // RecordServiceImpl.java
    @Override
    public IPage<Record> pageByCondition(IPage<Record> page, Record record) {
        QueryWrapper<Record> wrapper = new QueryWrapper<>();
        if (record.getMaintenanceId() != null) {
            wrapper.eq("maintenance_id", record.getMaintenanceId());
        }
        if (record.getUserId() != null) {
            wrapper.eq("user_id", record.getUserId());
        }
        if (record.getRoomId() != null) {
            wrapper.eq("room_id", record.getRoomId());
        }
        if(record.getProblemDesc() != null)
        {
            wrapper.like("problem_desc", record.getProblemDesc());
        }
        if (StringUtils.hasLength(record.getStatus())) {
            wrapper.eq("status", record.getStatus());
        }

        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public AjaxResult customerReport(CustomerReport customerReport) {
        Record record = new Record();
        record.setUserId(customerReport.getUserId());
        record.setRoomId(customerReport.getRoomId());
        record.setProblemDesc(customerReport.getProblemDesc());
        record.setStatus("1"); // 待处理状态

        boolean saved = save(record);
        if (saved) {
            return AjaxResult.success("报修提交成功");
        } else {
            return AjaxResult.fail("报修提交失败");
        }
    }

}
